<?php
/**
*市场总监
*/
require("../include/init.php");
//市场总监只是北区北京这一个公司的
//市场总监中的数据概况是从area表中进行查取获得的
$mysql=mysql::getIns();

// $address=$_REQUEST['address'];

date_default_timezone_set('Asia/Shanghai');

$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
  $address=$v['areaname'];
  $maxtime=time();
  $mintime=$maxtime-10*60;
  // echo $maxtime . '<br />';
  // echo $mintime;
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";
  $allnow=$mysql->getOne($sql);
  // echo $allnow;
  // print_r($allnow);
  // exit;


  //统计当天累计人数数
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='" . $address . "'";
  $allday=$mysql->getOne($sql);

  //首次今天首次进店人数,通过字段visittimes为1来进行判断
  $sql="select count(*) from alldev where whitelist <> '1' and visittimes=1 and timestamp > $mintime and timestamp < $maxtime and shop='".$address ."'";
  $firstlogin=$mysql->getOne($sql);

  //统计当前销售区
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."' and area in ('展厅区','销售区','精品区')";
  $sale=$mysql->getOne($sql);
  // echo $sale;
  // print_r($sale);
  // exit;

  //统计当前售后区
  $service=$allnow-$sale;


  //今天多次进店人数
  $sql="select count(*) from alldev where whitelist <> '1' and visittimes <> 1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $secondlogin=$mysql->getOne($sql);

  //统计登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $login=$mysql->getOne($sql);
  // echo $login;

  //统计未登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=2 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $nolog=$mysql->getOne($sql);
  // echo $nolog;

  //统计历史进店人数
  $sql="select count(*) from alldev where whitelist <> '1' and shop='". $address ."'";
  $history=$mysql->getOne($sql);

  // $data=include('./include/history.inc.php');
  // print_r($data);







  //根据address取出本店的数据来

  // $sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";

  //获取今天凌晨的时间戳
  $today = strtotime(date('Y-m-d', time()));

  //获取早晨9点的时间戳
  $now9=$today+9*60*60;
  // echo date('Y-m-d H:i:s',$now9);

  //获取今天早晨10点的时间戳
  $now10=$today+10*60*60;
  $now11=$today+11*60*60;
  $now12=$today+12*60*60;
  $now13=$today+13*60*60;
  $now14=$today+14*60*60;
  $now15=$today+15*60*60;
  $now16=$today+16*60*60;
  $now17=$today+17*60*60;
  $now18=$today+18*60*60;
  $now19=$today+19*60*60;

  //9点时的人数为
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now9 and timestamp < $now10 and shop='". $address ."'";
  $p9=$mysql->getOne($sql);
  // echo $p9;

  //10点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now10 and timestamp < $now11 and shop='". $address ."'";
  $p10=$mysql->getOne($sql);

  //11点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now11 and timestamp < $now12 and shop='". $address ."'";
  $p11=$mysql->getOne($sql);


  //12点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now12 and timestamp < $now13 and shop='". $address ."'";
  $p12=$mysql->getOne($sql);


  //13点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now13 and timestamp < $now14 and shop='". $address ."'";
  $p13=$mysql->getOne($sql);


  //14点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now14 and timestamp < $now15 and shop='". $address ."'";
  $p14=$mysql->getOne($sql);


  //15点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now15 and timestamp < $now16 and shop='". $address ."'";
  $p15=$mysql->getOne($sql);


  //16点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now16 and timestamp < $now17 and shop='". $address ."'";
  $p16=$mysql->getOne($sql);

  //17点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now17 and timestamp < $now18 and shop='". $address ."'";
  $p17=$mysql->getOne($sql);


  //18点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $now18 and timestamp < $now19 and shop='". $address ."'";
  $p18=$mysql->getOne($sql);
  // exit;


  //昨天9点时间戳
  $yesd9=$today+9*60*60-24*60*60;
  $yesd10=$today+10*60*60-24*60*60;
  $yesd11=$today+11*60*60-24*60*60;
  $yesd12=$today+12*60*60-24*60*60;
  $yesd13=$today+13*60*60-24*60*60;
  $yesd14=$today+14*60*60-24*60*60;
  $yesd15=$today+15*60*60-24*60*60;
  $yesd16=$today+16*60*60-24*60*60;
  $yesd17=$today+17*60*60-24*60*60;
  $yesd18=$today+18*60*60-24*60*60;
  $yesd19=$today+19*60*60-24*60*60;


  //昨天9点时的人数为
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd9 and timestamp < $yesd10 and shop='". $address ."'";
  $b9=$mysql->getOne($sql);
  // echo $p9;

  //昨天10点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd10 and timestamp < $yesd11 and shop='". $address ."'";
  $b10=$mysql->getOne($sql);

  //昨天11点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd11 and timestamp < $yesd12 and shop='". $address ."'";
  $b11=$mysql->getOne($sql);


  //昨天12点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd12 and timestamp < $yesd13 and shop='". $address ."'";
  $b12=$mysql->getOne($sql);


  //昨天13点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd13 and timestamp < $yesd14 and shop='". $address ."'";
  $b13=$mysql->getOne($sql);


  //昨天14点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd14 and timestamp < $yesd15 and shop='". $address ."'";
  $b14=$mysql->getOne($sql);


  //昨天15点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd15 and timestamp < $yesd16 and shop='". $address ."'";
  $b15=$mysql->getOne($sql);


  //昨天16点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd16 and timestamp < $yesd17 and shop='". $address ."'";
  $b16=$mysql->getOne($sql);

  //昨天17点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd17 and timestamp < $yesd18 and shop='". $address ."'";
  $b17=$mysql->getOne($sql);


  //昨天18点时的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $yesd18 and timestamp < $yesd19 and shop='". $address ."'";
  $b18=$mysql->getOne($sql);
  // echo $b18;

  /***********
  *获取上周时间
  *
  **********/
  $date=date('Y-m-d');  //当前日期
  $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
  $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
  $last_start=date('Y-m-d',strtotime("$now_start - 7 days"));  //上周开始日期
  $last_end=date('Y-m-d',strtotime("$now_start - 1 days"));  //上周结束日期
  $one=strtotime($last_start);
  $two=$one+24*3600*1;
  $three=$one+24*3600*2;
  $four=$one+24*3600*3;
  $five=$one+24*3600*4;
  $six=$one+24*3600*5;
  $seven=$one+24*3600*6;
  $nowone=$one+24*3600*7;

  //获取上周周一的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $one and timestamp < $two and shop='". $address ."'";
  $onep=$mysql->getOne($sql);
  // echo $one;
  // exit;

  //获取上周周二的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $two and timestamp < $three and shop='". $address ."'";
  $twop=$mysql->getOne($sql);

  //获取上周周三的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $three and timestamp < $four and shop='". $address ."'";
  $threep=$mysql->getOne($sql);


  //获取上周周四的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $four and timestamp < $five and shop='". $address ."'";
  $fourp=$mysql->getOne($sql);


  //获取上周周五的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $five and timestamp < $six and shop='". $address ."'";
  $fivep=$mysql->getOne($sql);

  //获取上周周六的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and shop='". $address ."'";
  $sixp=$mysql->getOne($sql);

  //获取上周周日的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $seven and timestamp < $nowone and shop='". $address ."'";
  $sevenp=$mysql->getOne($sql);
  // echo $sevenp;
  // exit;


  //获取上个月第一天时间
  $first =mktime(0,0,0,date('m',time())-1,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$first);
  //获取上个月第二周时间
  $second=$first+7*24*3600;
  // echo date('Y-m-d H:i:s',$second);

  //获取上个月第三周时间
  $third=$second+7*24*3600;

  //获取上个月第四周时间
  $fourth=$third+7*24*3600;
  // echo date('Y-m-d H:i:s',$fourth);

  //获取上个月第五周时间
  $fifth =mktime(0,0,0,date('m',time()),1,date('Y',time()));
  // $fifth=$fourth+7*24*3600;
  // echo date('Y-m-d H:i:s',$fifth);
  // exit;


  //获取上个月第一周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $first and timestamp < $second and shop='". $address ."'";
  $firstp=$mysql->getOne($sql);
  // echo $one;
  // exit;

  //获取上月第二周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $second and timestamp < $third and shop='". $address ."'";
  $secondp=$mysql->getOne($sql);

  //获取上月第三周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $third and timestamp < $fourth and shop='". $address ."'";
  $thirdp=$mysql->getOne($sql);


  //获取上月第四周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourth and timestamp < $fifth and shop='". $address ."'";
  $fourthp=$mysql->getOne($sql);


  //获取第一季度开始时间戳
  $firstmonth =mktime(0,0,0,1,1,date('Y',time()));

  //获取第二季度开始时间
  $secondmonth =mktime(0,0,0,4,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$secondmonth);

  //获取第三季度开始时间
  $thirdmonth =mktime(0,0,0,7,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$thirdmonth);

  //获取第四季度开始时间
  $fourthmonth =mktime(0,0,0,10,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$fourthmonth);

  //获取明年第一季度开始时间
  $firstmonths =mktime(0,0,0,1,1,date('Y',time())+1);
  // echo date('Y-m-d H:i:s',$firstmonths);



  //获取第一季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $firstmonth and timestamp < $secondmonth and shop='". $address ."'";
  $firstmonth=$mysql->getOne($sql);
  // echo $one;
  // exit;

  //获取第二季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $secondmonth and timestamp < $thirdmonth and shop='". $address ."'";
  $secondmonth=$mysql->getOne($sql);

  //获取第三季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $thirdmonth and timestamp < $fourthmonth and shop='". $address ."'";
  $thirdmonth=$mysql->getOne($sql);
  // echo $thirdmonth;
  // exit;


  //获取第四季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourthmonth and timestamp < $firstmonths and shop='". $address ."'";
  $fourthmonth=$mysql->getOne($sql);

  // echo $fourthmonth;
  // exit;






  /**********************************************************************

  							历史集客环比


  **********************************************************************/


  //统计集客比今天人数
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='". $address ."'";
  $nowjk=$mysql->getOne($sql);


  //统计昨天集客比
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t)-1,date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t)-1,date("Y",$t));
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='". $address ."'";
  $yesdjk=$mysql->getOne($sql);
  // echo $yesdjk;
  // exit;



  /****************
  *获取本周开始时间和结束时间
  *
  ***********/

  $date=date('Y-m-d');  //当前日期
  $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
  $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
  $one=strtotime($now_start);
  $two=$one+24*3600*1;
  $three=$one+24*3600*2;
  $four=$one+24*3600*3;
  $five=$one+24*3600*4;
  $six=$one+24*3600*5;
  $seven=$one+24*3600*6;
  $nowone=$one+24*3600*7;


  //统计本周周一集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $one and timestamp < $two and shop='". $address ."'";
  $onejk=$mysql->getOne($sql);
  //统计本周周二集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $two and timestamp < $three and shop='". $address ."'";
  $twojk=$mysql->getOne($sql);
  //统计本周周三集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $three and timestamp < $four and shop='". $address ."'";
  $threejk=$mysql->getOne($sql);
  //统计本周周四集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $four and timestamp < $five and shop='". $address ."'";
  $fourjk=$mysql->getOne($sql);
  //统计本周周五集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $five and timestamp < $six and shop='". $address ."'";
  $fivejk=$mysql->getOne($sql);

  //统计本周周六集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and shop='". $address ."'";
  $sixjk=$mysql->getOne($sql);
  // echo $sixjk;
  // exit;
  //统计本周周日集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $seven and timestamp < $nowone and shop='". $address ."'";
  $sevenjk=$mysql->getOne($sql);




  //统计本月人数

  /**************************
  获取本月第一个周一的时间

  **********************/
  $year=date('Y',time());
  $month=date('m',time());
  $base_time = strtotime ( "{$year}-{$month}-01" );
  $base_w = date ( "w", $base_time );

  if($base_w == '1'){
    //刚好是星期一
    $first = $base_time;
  }else{
    //不是星期一
    //找到上个星期天是几月几号
    $last_sun = date ( "Y-m-d", strtotime("-$base_w day", $base_time));
    //在上个星期天的基础上 加8天
    $first = strtotime ( "+8 day", strtotime ( $last_sun ) );
  }
  //第二个周一的时间
  $second=$first+7*24*3600;
  // echo date('Y-m-d H:i:s',$second);
  // exit;
  //第三个时间戳
  $third=$second+7*24*3600;
  // echo date('Y-m-d H:i:s',$third);
  // exit;
  //第四个时间戳
  $fourth=$third+7*24*3600;
  // echo date('Y-m-d H:i:s',$fourth);
  // exit;
  //下周一时间戳
  $fifth=$fourth+7*24*3600;
  // echo date('Y-m-d H:i:s',$third);
  // exit;





  //获取这个月第一周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $first and timestamp < $second and shop='". $address ."'";
  $firstjk=$mysql->getOne($sql);
  // echo $one;
  // exit;

  //获取这个月第二周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $second and timestamp < $third and shop='". $address ."'";
  $secondjk=$mysql->getOne($sql);
  // echo $secondjk;
  // exit;
  $bak=$secondjk;
  //获取这个月第三周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $third and timestamp < $fourth and shop='". $address ."'";
  $thirdjk=$mysql->getOne($sql);
  // echo $thirdjk;
  // exit;

  //获取这个月第四周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourth and timestamp < $fifth and shop='". $address ."'";
  $fourthjk=$mysql->getOne($sql);



  /*******

  年集客比用上述计算的即可,和上边中的年中的一样

  ******/


  /*************************************************



  同区集客对比



  **************************************************/


  //今天集客为今天累计人数 $allday

  //全区集客平均值   因为字段中没有区域,暂时只有一个店,平均值与今天的一样



  if($address=='成都新元素雅麓汽车销售服务有限公司'||$address=='云南驰泰汽车销售服务有限公司'||
  	$address=='宁夏奥立升汽车销售服务有限公司'||$address=='陕西奥诚汽车销售服务有限公司'){



      /****************
      *获取本周开始时间和结束时间
      *
      ***********/

      $date=date('Y-m-d');  //当前日期
      $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
      $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
      $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
      $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
      $one=strtotime($now_start);
      $two=$one+24*3600*1;
      $three=$one+24*3600*2;
      $four=$one+24*3600*3;
      $five=$one+24*3600*4;
      $six=$one+24*3600*5;
      $seven=$one+24*3600*6;
      $nowone=$one+24*3600*7;

  	//本周的集客比
  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $end and shop='成都新元素雅麓汽车销售服务有限公司' or shop='云南驰泰汽车销售服务有限公司'
  	or shop='宁夏奥立升汽车销售服务有限公司' or shop='陕西奥诚汽车销售服务有限公司'";
  	$week=$mysql->getOne($sql);
  	// echo $week;
  	// exit;

  	//区域平均值于此一样


  	//本月的集客比



  	//获取这个个月第一天时间
  	$first =mktime(0,0,0,date('m',time()),1,date('Y',time()));
  	// echo date('Y-m-d H:i:s',$first);
  	// exit;


  	//获取这个月第五周时间
  	$fifth =mktime(0,0,0,date('m',time())+1,1,date('Y',time()));
  	// echo date('Y-m-d H:i:s',$fifth);
  	// exit;

  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and shop='成都新元素雅麓汽车销售服务有限公司' or shop='云南驰泰汽车销售服务有限公司'
  	or shop='宁夏奥立升汽车销售服务有限公司' or shop='陕西奥诚汽车销售服务有限公司'";
  	$month=$mysql->getOne($sql);
  	// echo $month;
  	// exit;

  	//本月平均值于此一样

  	//今年第一天和最后一天时间
  	$year=date("Y",time());
  	$firsttime=$year."-01-01";
  	// echo $first;
  	$firsttime=strtotime($firsttime);
  	// echo date('Y-m-d H:i:s',$first);
  	// exit;
  	$years=$year+1;
  	$endtime=$years."-01-01";
  	$endtime=strtotime($endtime);
  	// echo date('Y-m-d H:i:s',$end);
  	// exit;

  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and shop='成都新元素雅麓汽车销售服务有限公司' or shop='云南驰泰汽车销售服务有限公司'
  	or shop='宁夏奥立升汽车销售服务有限公司' or shop='陕西奥诚汽车销售服务有限公司'";
  	$years=$mysql->getOne($sql);
  	//今天平均值
  	$avgallday=ceil($allday/4);
  	//周平均值
  	$avgweek=ceil($week/4);
  	//月平均值
  	$avgmonth=ceil($month/4);
  	//年平均值
  	$avgyears=ceil($years/4);
    /****************
    *获取本周开始时间和结束时间
    *
    ***********/

    $date=date('Y-m-d');  //当前日期
    $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
    $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
    $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
    $one=strtotime($now_start);
    $two=$one+24*3600*1;
    $three=$one+24*3600*2;
    $four=$one+24*3600*3;
    $five=$one+24*3600*4;
    $six=$one+24*3600*5;
    $seven=$one+24*3600*6;
    $nowone=$one+24*3600*7;

  	//本周的集客比
  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $end and shop='" . $address . "'";
  	$week=$mysql->getOne($sql);

  	// echo $week;
  	// exit;
  	// echo $week;
  	// exit;

  	//区域平均值于此一样


  	//本月的集客比



  	//获取这个个月第一天时间
  	$first =mktime(0,0,0,date('m',time()),1,date('Y',time()));
  	// echo date('Y-m-d H:i:s',$first);
  	// exit;


  	//获取这个月第五周时间
  	$fifth =mktime(0,0,0,date('m',time())+1,1,date('Y',time()));
  	// echo date('Y-m-d H:i:s',$fifth);
  	// exit;

  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and shop='" . $address . "'";
  	$month=$mysql->getOne($sql);
  	// echo $month;
  	// exit;

  	//本月平均值于此一样

  	//今年第一天和最后一天时间
  	$year=date("Y",time());
  	$firsttime=$year."-01-01";
  	// echo $first;
  	$firsttime=strtotime($firsttime);
  	// echo date('Y-m-d H:i:s',$first);
  	// exit;
  	$years=$year+1;
  	$endtime=$years."-01-01";
  	$endtime=strtotime($endtime);
  	// echo date('Y-m-d H:i:s',$end);
  	// exit;

  	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and shop='" . $address . "'";
  	$years=$mysql->getOne($sql);
  }else{


    /****************
    *获取本周开始时间和结束时间
    *
    ***********/

    $date=date('Y-m-d');  //当前日期
    $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
    $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
    $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
    $one=strtotime($now_start);
    $two=$one+24*3600*1;
    $three=$one+24*3600*2;
    $four=$one+24*3600*3;
    $five=$one+24*3600*4;
    $six=$one+24*3600*5;
    $seven=$one+24*3600*6;
    $nowone=$one+24*3600*7;

  //本周的集客比
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $end and shop='" . $address . "'";
  $week=$mysql->getOne($sql);

  // echo $week;
  // exit;
  // echo $week;
  // exit;

  //区域平均值于此一样


  //本月的集客比



  //获取这个个月第一天时间
  $first =mktime(0,0,0,date('m',time()),1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$first);
  // exit;


  //获取这个月第五周时间
  $fifth =mktime(0,0,0,date('m',time())+1,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$fifth);
  // exit;

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and shop='" . $address . "'";
  $month=$mysql->getOne($sql);
  // echo $month;
  // exit;

  //本月平均值于此一样

  //今年第一天和最后一天时间
  $year=date("Y",time());
  $firsttime=$year."-01-01";
  // echo $first;
  $firsttime=strtotime($firsttime);
  // echo date('Y-m-d H:i:s',$first);
  // exit;
  $years=$year+1;
  $endtime=$years."-01-01";
  $endtime=strtotime($endtime);
  // echo date('Y-m-d H:i:s',$end);
  // exit;

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and shop='" . $address . "'";
  $years=$mysql->getOne($sql);
  // echo $years;
  // exit;

  $avgallday=$allday;
  	//周平均值
  	$avgweek=$week;
  	//月平均值
  	$avgmonth=$month;
  	//年平均值
  	$avgyears=$years;
  }

  /********************************************************************

  全国集客对比


  ***********************************************************************/

  //本店值月上述的一样

  //全国今天的值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop <> 'Audi City BeiJing'";
  $alldaycountry=$mysql->getOne($sql);
  // echo $alldaycountry;
  // exit;
  $sql="select shop from alldev group by shop";
  $num=$mysql->getAll($sql);
  // print_r($num);
  $num=count($num)-1;
  // echo $num;
  // exit;
  //全国今天的平均值
  $daycountry=ceil($alldaycountry/$num);
  // echo $daycountry;
  // exit;
  //全国本周的值

  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $one and timestamp < $end and shop <> 'Audi City BeiJing'";
  $allweekcountry=$mysql->getOne($sql);
  //全国本周的平均值
  $weekcountry=ceil($allweekcountry/$num);


  //全国本月的平均值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $first and timestamp < $fifth and shop <> 'Audi City BeiJing'";
  $allmonthcountry=$mysql->getOne($sql);
  $monthcountry=ceil($allmonthcountry/$num);


  //全国本年的值
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $firsttime and timestamp < $endtime and shop <> 'Audi City BeiJing'";
  $allyearscountry=$mysql->getOne($sql);
  $yearcountry=ceil($allyearscountry/$num);

  //分别写入redis
  switch ($address) {
    case '义乌市博奥汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_ywba';
      break;
    case '广东粤奥汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_gdya';
      break;
    case '辽宁鑫迪汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_lnxd';
      break;
    case '江苏天奥汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_jsta';
      break;
    case '成都新元素雅麓汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_cdyl';
      break;
    case '云南驰泰汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_ynct';
      break;
    case '宁夏奥立升汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_nxals';
      break;
    case '陕西奥诚汽车销售服务有限公司':
      $hash='4S_MarketDirector_main_sxac';
      break;
    case '北京博瑞祥星汽车销售有限公司':
      $hash='4S_MarketDirector_main_brxx';
      break;
  }
  // echo $hash;
  $redis = new Redis();
  //连接服务器
  $redis->connect("localhost");
  //授权
  $redis->auth("redis=pwd@2016");

  $redis->hmset($hash,
  array(
  'allnow' =>$allnow,
  'allday'=>$allday,
  'firstlogin'=>$firstlogin,
  'sale'=>$sale,
  'service'=>$service,
  'secondlogin'=>$secondlogin,
  'login'=>$login,
  'nolog'=>$nolog,
  'history'=>$history,
  'p9'=>$p9,
  'p10'=>$p10,
  'p11'=>$p11,
  'p12'=>$p12,
  'p13'=>$p13,
  'p14'=>$p14,
  'p15'=>$p15,
  'p16'=>$p16,
  'p17'=>$p17,
  'p18'=>$p18,
  'b9'=>$b9,
  'b10'=>$b10,
  'b11'=>$b11,
  'b12'=>$b12,
  'b13'=>$b13,
  'b14'=>$b14,
  'b15'=>$b15,
  'b16'=>$b16,
  'b17'=>$b17,
  'b18'=>$b18,
  'onep'=>$onep,
  'twop'=>$twop,
  'threep'=>$threep,
  'fourp'=>$fourp,
  'fivep'=>$fivep,
  'sixp'=>$sixp,
  'sevenp'=>$sevenp,
  'firstp'=>$firstp,
  'secondp'=>$secondp,
  'thirdp'=>$thirdp,
  'fourthp'=>$fourthp,
  'firstmonth'=>$firstmonth,
  'secondmonth'=>$secondmonth,
  'thirdmonth'=>$thirdmonth,
  'fourthmonth'=>$fourthmonth,
  'nowjk'=>$nowjk,
  'yesdjk'=>$yesdjk,
  'onejk'=>$onejk,
  'twojk'=>$twojk,
  'threejk'=>$threejk,
  'fourjk'=>$fourjk,
  'fivejk'=>$fivejk,
  'sixjk'=>$sixjk,
  'sevenjk'=>$sevenjk,
  'firstjk'=>$firstjk,
  'secondjk'=>$secondjk,
  'thirdjk'=>$thirdjk,
  'fourthjk'=>$fourthjk,
  'firstmonth'=>$firstmonth,
  'secondmonth'=>$secondmonth,
  'thirdmonth'=>$thirdmonth,
  'fourthmonth'=>$fourthmonth,
  'daycountry'=>$daycountry,
  'week'=>$week,
  'month'=>$month,
  'years'=>$years,
  'weekcountry'=>$weekcountry,
  'monthcountry'=>$monthcountry,
  'yearcountry'=>$yearcountry,
  ));
}
